//V2.3 2021-10-25
switchID = 0; //switchID用于全局 0为DDL 1为通知 2为全部

start = 0; //查询数据库的开始位置
available = true; //是否可继续查询
scrollEnable = true; //防止反复触发查询
boxList = {};

const stateStringList = Array('紧急', '未过期', '已过期', '抛弃');
const stateStyleList = Array('warning', '', 'end', 'deserted');
const typeList = Array('DDL', '活动');
const STEP = 20;//每次查询数
function Enablescroll() {
    scrollEnable = true;
}

function tit($inde) //显示详情
{
    $("#" + $inde + "a").css("display", "none");
    $("#" + $inde + "b").css("display", "block");
}

function detail_($inde) //显示标题
{
    $("#" + $inde + "a").css("display", "block");
    $("#" + $inde + "b").css("display", "none");
} //刷新状态函数

function vanish() {

    $("#statusLabel").css("display", "none");

}

function switchList($id) { //切换DDL与通知
    switchID = $id; //switchID用于全局 0为DDL 1为通知

    //使所有选项标签变灰
    for (i = 0; i < 3; i++) {
        $("#selectorS" + i).removeClass("selectorActive");
        $("#selectorS" + i).addClass("selectorInactive");
    }

    //选项变蓝
    $("#selectorS" + switchID).removeClass("selectorInactive");
    $("#selectorS" + switchID).addClass("selectorActive");

    if (switchID == 0) {
        //DDL
        $("#selectorClass").css("display", "inline");


    } else {
        //非DDL
        $("#selectorClass").css("display", "none"); //隐藏班级选项
    }

    start = 0; //查询数据库的开始位置
    available = true; //是否可继续查询
    scrollEnable = true; //防止反复触发查询
    boxList = {};
    add();
}


function add() {
    $_classes = $('select option:selected').val();
    $queryURL = "php/get_list.php"; //生成URL
    class_List = new Array("0", "1", "*");
    var classBy = 'ddl';
    var queryAll = false;
    if (switchID == 2) {
        classBy = 'time';
        queryAll = true;
    }
    if (switchID == 1 | switchID == 2) {
        //活动或全部
        $_classes = 'dddd';
    }
    //console.log(_classes);
    $("#statusLabel").css("display", "block");
    $("#statusLabel").html("正在刷新...");
    $("#statusLabel").css("color", "");
    $("#msg").html("加载中...");

    $.get($queryURL, {
        name: $_classes,
        class_: class_List[switchID],
        queryAll: queryAll,
        start: start,
        step: STEP
    }, function (data) {

        var obj = JSON.parse(data); //由JSON字符串转换为JSON对象
        if (obj.length == 0) {
            available = false;
            scrollEnable = false;
            $("#msg").html("已加载全部");
        } else {
            for (i = 0; i < obj.length; i++) {

                if (classBy == "ddl") {
                    if (obj[i]["state"] != "3") {
                        if (!Array.isArray(boxList[obj[i][classBy].substring(0, 10)])) {
                            boxList[obj[i][classBy].substring(0, 10)] = new Array();
                        }

                        boxList[obj[i][classBy].substring(0, 10)].push(obj[i]);
                    }
                }
                else {
                    if (!Array.isArray(boxList[obj[i][classBy].substring(0, 10)])) {
                        boxList[obj[i][classBy].substring(0, 10)] = new Array();
                    }

                    boxList[obj[i][classBy].substring(0, 10)].push(obj[i]);
                }
            }


            $("#msg").html("");
            $("#list").html("");
            if (classBy == "time") {
                $("#list").append("<div class='date'>发布时间</div>");
            }


            for (let key in boxList) {
                $("#list").append("<div class='date'>" + key + "</div>");

                for (i = 0; i < boxList[key].length; i++) {
                    sta = boxList[key][i]['state'];
                    stateString = (switchID == 2) ? typeList[boxList[key][i]['class_']] : stateStringList[sta];
                    stateStyle = (classBy == "ddl") ? stateStyleList[sta] : "";

                    $("#list").append(
                        "<div onclick='tit(" + boxList[key][i]['index_'] + ")'>" +
                        "<div class='box " + stateStyle + "' id='" + boxList[key][i]['index_'] + "a'>" + boxList[key][i]['content'] +
                        "<br/><div class='small'>" + boxList[key][i][(classBy == "ddl") ? 'ddl' : 'time'] + "&nbsp&nbsp|&nbsp&nbsp" + stateString + "</div></div>"
                    );

                    //详情
                    $("#list").append(
                        "<div onclick='detail_(" + boxList[key][i]['index_'] + ")' >" +
                        "<div  style='display: none' class='detail' id='" + boxList[key][i]['index_'] + "b'>" + boxList[key][i]['detail'] + "</div>" +
                        "</div>"
                    );
                }
                // console.log(key);
                // console.log(boxList[key]);
            }
        }


        $("#statusLabel").html(getTime() + " 刷新成功");
        setTimeout(vanish, 3000);
        $("#statusLabel").css("color", "");


        
        //遍历detail
        $("#list div.detail").each(function () {
            $(this).html(changeUrl($(this).text()));
        });

        // //活动的标题不变色
        // if (switchID == 1) {
        //     $("div.box").addClass("activity");
        // } else {
        //     $("div.box").removeClass("activity");
        // }

    }).fail(function () {
        $("#statusLabel").html("刷新失败");
        $("#statusLabel").css("color", "red");
    });

    setTimeout(Enablescroll, 500);
}

$(document).ready(function () {
    var cla = getUrlParam("cla");
    if (!cla) {
        cla = 'dddd';
    }

    $('#selectorClass').val(cla);
    switchList(0);
});

$(window).scroll(function () {
    if (scrollEnable) {

        var scrollTop = $(window).scrollTop();
        var scrollHeight = $(document).height();
        var windowHeight = $(window).height();
        //console.log(scrollTop, windowHeight, scrollHeight);
        // $("#test").html(scrollTop+" "+windowHeight+" "+scrollHeight);
        // if (scrollTop == 0) {
        //     alert('触顶');
        // }

        if (scrollTop + windowHeight >= scrollHeight - 100) {
            scrollEnable = false;
            // alert('下拉到底了');
            if (available) {
                $("#msg").html("加载中...");
                start += STEP;
                add();
            }
        }
    }
})